package com.idanchuang.component.just.web.warmup;

import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.core.ServiceInfo;
import com.idanchuang.sun.client.common.health.WarmUp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *
 * 预热web容器
 *
 * @author yjy
 * Created at 2022/2/9 11:43 下午
 */
@RestController
public class ComponentJustWebWarmUp {

    public static final String WARM_UP_MAPPING_GET = "/component/warmup/get";
    public static final String WARM_UP_MAPPING_POST = "/component/warmup/post";

    @Autowired
    private RestTemplate restTemplate;

    @WarmUp
    public void warmup() {
        String server = "http://" + ServiceInfo.address();
        restTemplate.getForObject(server + WARM_UP_MAPPING_GET, String.class);
        restTemplate.postForObject(server + WARM_UP_MAPPING_POST, "hello world", String.class);
    }

    @GetMapping(WARM_UP_MAPPING_GET)
    public JsonResult<Void> warmupGet() {
        return JsonResult.OK;
    }

    @PostMapping(WARM_UP_MAPPING_POST)
    public JsonResult<String> warmupPost(@RequestBody String body) {
        return JsonResult.success(body);
    }

}
